package com.wonders.xianningsocialinsurance.activities;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;

import com.baidu.mapapi.SDKInitializer;
import com.wonders.xianningsocialinsurance.App;
import com.wonders.xianningsocialinsurance.R;
import com.wonders.xianningsocialinsurance.adapters.HomeFragmentAdapter;
import com.wonders.xianningsocialinsurance.fragments.MenuFragment;
import com.wonders.xianningsocialinsurance.fragments.MessageFragment;
import com.wonders.xianningsocialinsurance.fragments.NearbyFragment;
import com.wonders.xianningsocialinsurance.fragments.UserDetailFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private Toolbar toolbar;
    private RadioGroup bottom_tab_radioGroup;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        if (Integer.valueOf(Build.VERSION.SDK_INT)>=23){
            askPermission();
        }
        registerUI();
    }

    //初始化注册所有ui组件
    @Override
    public void registerUI() {
        initToolBar();
        initCollapseLayout();
        setRadioGroup(0);
        initViewPager();

    }
    //初始化设置collapse
    private void initCollapseLayout(){
        collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapse_layout);
        collapsingToolbarLayout.setBackgroundResource(R.mipmap.banner1);
        bottom_tab_radioGroup = (RadioGroup)findViewById(R.id.bottom_tab);
        bottom_tab_radioGroup.setOnCheckedChangeListener(this);
    }
    //初始化设置toolbar
    private void initToolBar(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.app_name);
        setSupportActionBar(toolbar);
    }
    //初始化fragment
    private List<Fragment> initFragments(){
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new MenuFragment().newInstance("https://3g.163.com/touch/#/"));
        fragmentList.add(new MenuFragment().newInstance("https://3g.163.com/touch/#/"));
        fragmentList.add(new MenuFragment().newInstance("https://3g.163.com/touch/#/"));
        fragmentList.add(new MenuFragment().newInstance("https://3g.163.com/touch/#/"));
        return fragmentList;
    }
    //初始化tabbar
    private void setRadioGroup(int which){
        bottom_tab_radioGroup.clearCheck();
        for (int i=0;i<4;i++){
            if (which==i){
                bottom_tab_radioGroup.getChildAt(i).setSelected(true);
            }else {
                bottom_tab_radioGroup.getChildAt(i).setSelected(false);
            }
        }
        setToolBar(which);
    }

    private void setToolBar(int position){
//        collapsingToolbarLayout.setBackgroundResource(App.toolbar_background_res[position]);
        collapsingToolbarLayout.setTitle(getString(App.tab_name[position]));
        collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.transparent));
    }
    //初始化viewPager
    private void initViewPager(){
        viewPager = (ViewPager)findViewById(R.id.view_pager);
        viewPager.setAdapter(new HomeFragmentAdapter(getSupportFragmentManager(),initFragments()));
        viewPager.setOffscreenPageLimit(4);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                setRadioGroup(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    /**
     * 针对6.0以上版本的
     * 新的权限申请方式
     */
    @TargetApi(Build.VERSION_CODES.M)
    private void askPermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[] {Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE},
                    123);
            return;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 123:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        registerUI();
                } else {
                    finish();
                }
                return;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.inspection_radio:
                viewPager.setCurrentItem(0);
                break;
            case R.id.query_radio:
                viewPager.setCurrentItem(2);
                break;
            case R.id.nearby_radio:
                viewPager.setCurrentItem(1);
                break;
            case R.id.user_details_radio:
                viewPager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }
}
